<?php
//attempt to lookup character. If it fails, exit the script to prevent further errors.
try
{
	$character = $api->getCharacterApi()->getCharacter($realm, $char, 
		array('guild', 'stats', 'items', 'titles', 'professions', 'talents'));

	//set attributes into variables
	//all field names should be in single quotes for consistency

	//character attributes
	//add titles
	$name = $character['name'];
	$level = $character['level'];
	$points = $character['achievementPoints'];
	$lastLogin = date('Y-m-d',(substr($character['lastModified'],0,10)));
	$guildName = $character['guild']['name'];
	$race = $character['race'];
	$genderid = $character['gender'];
	$raceid = $character['race'];
	$classid = $character['class'];
	$itemLevel = $character['items']['averageItemLevel'];
	$charInactive = 0;

	//put possible races into arrays
	include 'raceClass.php';

	//set the faction and race by looking through the arrays
	if (array_key_exists($race,$horde))
	{
		$faction = 'Horde';
		$race = $horde[$race];
	}
	else
	{
		$faction = 'Alliance';
		$race = $alliance[$race];
	}

	//convert class number to name
	$class = $character['class'];

	//class_a to be found in included file

	$class = $class_a[$class];


	//build character image general and URL to use on other pages
	//e.g. http://us.battle.net/static-render/us/magtheridon/207/65951439-avatar.jpg
	$image = $character['thumbnail'];
	$imgUrl = "http://us.battle.net/static-render/us/".str_replace("225", "56", $image);
	//replace single quotes to allow armory url to work correctly
	//can we use the realm slug here?
	$realmUrl = str_replace("'","",$realm);
	
	//item url
	//http://us.media.blizzard.com/wow/icons/
	//build image url as a variable to use on other pages
	$armoryUrl = "<a target='_blank' href='http://us.battle.net/wow/en/character/" . $realmUrl . "/" .
		$char . "/simple'><img src='" . $imgUrl . "' height='100'/></a>";

	//$talent1 = $character[talents][0][name];
	$characterTalents = $character['talents'];
	$talent1 = xml_blank($characterTalents, 0, 'name');
	$talent2 = xml_blank($characterTalents, 1, 'name');

	//put skills and professions into several arrays
	//first professions...
	$characterProfs = $character['professions']['primary'];
	
	//all professions with recipes
	$profArray = array(164 => 'Blacksmithing',165 => 'Leatherworking',171 => 'Alchemy',197 => 'Tailoring',
	202 => 'Engineering',333 => 'Enchanting',755 => 'Jewelcrafting',773 => 'Inscription',185 => 'Cooking', 129=>'First Aid');

	$recipeProfs = array(1=>164, 2=>165, 3=>171, 4=>197, 5=>202, 6=>333, 7=>755, 8=>773, 9=>185, 10=>129);

	//skill name
	$prof = array ( 0 => xml_blank($characterProfs, 0, 'name'), 1 =>
		xml_blank($characterProfs, 1, 'name')
	);
	//skill level
	$profval = array ( 0 => xml_blank($characterProfs, 0, 'rank'), 1 =>
		xml_blank($characterProfs, 1, 'rank')
	);
	//max skill level
	$profmax = array ( 0 => xml_blank($characterProfs, 0, 'max'), 1 =>
		xml_blank($characterProfs, 1, 'max')
	);

	//secondary skills
	$characterSkills = $character['professions']['secondary'];

	$skill  = array (
		0 => xml_blank($characterSkills, 0, 'name'),
		1 => xml_blank($characterSkills, 1, 'name'),
		2 => xml_blank($characterSkills, 2, 'name'),
		3 => xml_blank($characterSkills, 3, 'name')
	);

	$skillval  = array (
		0 => xml_blank($characterSkills, 0, 'rank'),
		1 => xml_blank($characterSkills, 1, 'rank'),
		2 => xml_blank($characterSkills, 2, 'rank'),
		3 => xml_blank($characterSkills, 3, 'rank')
	);

	$skillmax  = array (
		0 => xml_blank($characterSkills, 0, 'max'),
		1 => xml_blank($characterSkills, 1, 'max'),
		2 => xml_blank($characterSkills, 2, 'max'),
		3 => xml_blank($characterSkills, 3, 'max')
	);


	//character stats
	//set general var first
	$characterStats = $character['stats'];
	//bars
	$health = $characterStats['health'];
	$bartype = $characterStats['powerType'];
	$regen_combat = $characterStats['mana5Combat'];
	$regen_nocombat = $characterStats['mana5'];
	$mana = $characterStats['power'];

	//strength
	$strength = $characterStats['str'];
	//$strength_att = $xml->characterInfo->characterTab->baseStats->strength->attributes()->attack;
	//$strength_blk = $xml->characterInfo->characterTab->baseStats->strength->attributes()->block;
	//$strength_base = $xml->characterInfo->characterTab->baseStats->strength->attributes()->base;
	//$strength_items = $strength-$strength_base;

	//agility
	$agility = $characterStats['agi'];
	//$agility_armor = $xml->characterInfo->characterTab->baseStats->agility->attributes()->armor;
	//$agility_att = $xml->characterInfo->characterTab->baseStats->agility->attributes()->attack;
	//$agility_base = $xml->characterInfo->characterTab->baseStats->agility->attributes()->base;
	//$agility_crit = $xml->characterInfo->characterTab->baseStats->agility->attributes()->critHitPercent;
	//$agility_items = $agility-$agility_base;

	//stamina
	$stamina = $characterStats['sta'];
	//$stamina_base = $xml->characterInfo->characterTab->baseStats->stamina->attributes()->base;
	//$stamina_health = $xml->characterInfo->characterTab->baseStats->stamina->attributes()->health;
	//$stamina_pet = $xml->characterInfo->characterTab->baseStats->stamina->attributes()->petBonus;
	//$stamina_items = $stamina-$stamina_base;

	//intellect
	$intellect = $characterStats['int'];
	//$intellect_base = $xml->characterInfo->characterTab->baseStats->intellect->attributes()->base;
	//$intellect_crit = $xml->characterInfo->characterTab->baseStats->intellect->attributes()->critHitPercent;
	//$intellect_mana = $xml->characterInfo->characterTab->baseStats->intellect->attributes()->mana;
	//$intellect_pet = $xml->characterInfo->characterTab->baseStats->intellect->attributes()->petBonus;
	//$intellect_items = $intellect-$intellect_base;

	//spirit
	$spirit = $characterStats['spr'];
	//$spirit_base = $xml->characterInfo->characterTab->baseStats->spirit->attributes()->base;
	//$spirit_health = $xml->characterInfo->characterTab->baseStats->spirit->attributes()->healthRegen;
	//$spirit_mana = $xml->characterInfo->characterTab->baseStats->spirit->attributes()->manaRegen;
	//$spirit_items = $spirit-$spirit_base;

	//armor
	$armor = $characterStats['armor'];
	//$armor_base = $xml->characterInfo->characterTab->baseStats->armor->attributes()->base;
	//$armor_percent = $xml->characterInfo->characterTab->baseStats->armor->attributes()->percent;
	//$armor_dmg_absorb = $health / (1-($armor_percent/100));
	//$armor_pet = $xml->characterInfo->characterTab->baseStats->armor->attributes()->petBonus;
	//$armor_items = $armor-$armor_base;

	//weapons and hand items
	$main_dps = $characterStats['mainHandDps'];
	$main_max = $characterStats['mainHandDmgMax'];
	$main_min = $characterStats['mainHandDmgMin'];
	$main_speed = $characterStats['mainHandSpeed'];

	$off_dps = $characterStats['offHandDps'];
	$off_max = $characterStats['offHandDmgMax'];
	$off_min = $characterStats['offHandDmgMin'];
	$off_speed = $characterStats['offHandSpeed'];

	$power = $characterStats['attackPower'];
	//$power_base = $xml->characterInfo->characterTab->melee->power->attributes()->base;
	//$power_items = $power-$power_base;
	//$power_dps = $xml->characterInfo->characterTab->melee->power->attributes()->increasedDps;

	$hitRating = $characterStats['hitRating'];
	//$hitRating_percent = $xml->characterInfo->characterTab->melee->hitRating->attributes()->increasedHitPercent;
	//$hitRating_penetration = $xml->characterInfo->characterTab->melee->hitRating->attributes()->penetration;
	//$hitRating_red_armor = $xml->characterInfo->characterTab->melee->hitRating->attributes()->reducedArmorPercent;

	$critchance_rating = $characterStats['critRating'];
	$critchance_percent = $characterStats['crit'];
	//$critchance_plus_percent = $xml->characterInfo->characterTab->melee->critChance->attributes()->plusPercent;

	//$expertise = $xml->characterInfo->characterTab->melee->expertise->attributes()->value;
	//$expertise_items = $xml->characterInfo->characterTab->melee->expertise->attributes()->additional;
	//$expertise_percent = $xml->characterInfo->characterTab->melee->expertise->attributes()->percent;
	//$expertise_rating = $xml->characterInfo->characterTab->melee->expertise->attributes()->rating;

	$ranged_dps = $characterStats['rangedDps'];
	$ranged_max = $characterStats['rangedDmgMax'];
	$ranged_min = $characterStats['rangedDmgMin'];
	$ranged_speed = $characterStats['rangedSpeed'];
	//$ranged_haste = $xml->characterInfo->characterTab->ranged->speed->attributes()->value;
	//$ranged_haste_percent = $xml->characterInfo->characterTab->ranged->speed->attributes()->hastePercent;
	//$ranged_haste_rating = $xml->characterInfo->characterTab->ranged->speed->attributes()->hasteRating;

	$ranged_power = $characterStats['rangedAttackPower'];
	//$ranged_power_base = $xml->characterInfo->characterTab->ranged->power->attributes()->base;
	//$ranged_power_items = $ranged_power-$ranged_power_base;
	//$ranged_power_dps = $xml->characterInfo->characterTab->ranged->power->attributes()->increasedDps;
	//$ranged_power_pet_att = $xml->characterInfo->characterTab->ranged->power->attributes()->petAttack;
	//$ranged_power_pet_spell = $xml->characterInfo->characterTab->ranged->power->attributes()->petSpell;

}
catch (exception $e)
{
	//$charInactive = 1;
	//echo '<p>Error occurred finding a toon :( ' . $char . "</p>";
	//exit(1);
}



?>